package leetcode101.ch3;
//LC 142

public class DetectCycleTowPoint {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        if (head == null) return null;
        while (true) {
            if (fast.next != null && fast.next.next != null && slow.next != null) {
                fast = fast.next.next;
                slow = slow.next;
                if (slow == fast) {
                    break;
                }
            }else {
                return null;
            }
        }
        fast = head;
        while (slow != fast){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;

    }
}
